<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of advsAction
 *
 * @author xlp
 */
class advsAction extends commonAction {

    private $cacheFilePre = null; //保存缓存信息的文件名前缀

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->status = array(1 => '正常', 0 => '<span class="red">下线</span>');
        $this->advsPath = array(
            array('name' => '头部', 'size' => '1200px*110px'),
            // array('name' => '左边', 'size' => '300px*350px'),
            // array('name' => '右边', 'size' => '300px*350px')
        );
        // $this->cacheFilePre = 'advs/city_id_' . admin::$adminInfo['city_id'];
        $this->assign(array('advsPath' => $this->advsPath, 'status' => $this->status));
    }

    function index() {
        $title = $this->_get('q');
        $stime = $this->_get('stime');
        $etime = $this->_get('etime');
        $p = $this->_getid('p', 1);
        $status = $this->_getid('status', 0);
        $positionId = $this->_getid('position_id', 0);
        $recycle = false; //是否回收站
        $where = array();
        if ($stime && $etime) {
            $stime = inTime($stime);
            $etime = inTime($etime) + (24 * 60 * 60) - 1;
        }
        if ($stime && $etime) {
            $where['infotime[>=]'] = $stime;
            $where['infotime[<=]'] = $etime;
        }
        if ($title) {
            $where['LIKE'] = array('title' => parent::safeSearch($title));
        }
        if ($positionId) {
            $where['position_id'] = $positionId - 1;
        }
        $rs = M('advs')->where($where)->page($p)->order('id desc')->findAll();
        if($rs) {
            foreach ($rs as $k => &$v) {
                $game = M('game')->field('name')->where(array('id' => $v['game_id']))->find();
                $v['game'] = $game['name'];
            }
        }
        $this->assign(array('rs' => $rs, 'p' => $p, 'total' => M('advs')->getTotal(), 'recycle' => $recycle));
        $this->display();
    }

    //编辑
    function edit() {
        $id = $this->_getid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('advs')->where(array('id' => $id))->find();
        $games = M('game')->findAll();
        $this->assign('games' , $games);
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    //添加
    function add() {
        $games = M('game')->findAll();
        $this->assign('games' , $games);
        $this->display('add');
    }

    //保存图文信息
    function post() {
        $objData = $this->_getpostData();
        //检查该广告位是否被占用
        if(!$this->checkGameAdvs($objData)) {
            $this->JsonReturn('该款游戏下已有在用广告位，暂时无法添加');
        }
        $id = M('advs')->insert($objData);
        // F($this->cacheFilePre, null); //清除缓存
        $this->JsonReturn('ok', $id, 1);
    }

    //保存修改
    function save() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('advs')->where(array('id' => $id))->find();
        if (!$rs) {
            showError('内容不存在');
        }
        $objData = $this->_getpostData();
        unset($objData['infotime']);
        M('advs')->update($objData, array('id' => $id));
        // F($this->cacheFilePre, null); //清除缓存
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        M('advs')->delete("id IN (" . $id . ") AND aid=" . admin::$adminInfo['aid']);
        F($this->cacheFilePre, null); //清除缓存
        $this->JsonReturn('ok', null, 1);
    }

    function setting() {
        $id = $this->_postid('id');
        $isOk = $this->_postid('is_ok', 0);
        $where = array();
        if (!$id) {
            showError('参数丢失');
        }
        if ($isOk) {
            $where = array('status' => 1);
        }
        if ($where) {
            M('advs')->update($where, array('id' => $id));
            F($this->cacheFilePre, null); //清除缓存
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('error');
        }
    }

    function clear() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $advs = M('advs')->where(array('id' => $id))->find();
        if (!$advs) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('advs')->delete(array('id' => $id));
        F($this->cacheFilePre, null); //清除缓存
        $this->JsonReturn('ok', null, 1);
    }

    //接收字段列表
    private function _getpostData() {
        $list = array(
            'aid' => 0,
            'position_id' => 0,
            'city_id' => 0,
            'game_id' => 0,
            'title' => '',
            'link_url' => '#',
            'img_url' => '',
            'content' => '',
            'stime' => 0,
            'etime' => 0,
            'status' => 0,
            'px' => 0,
            'infotime' => 0);
        $objData = array();
        foreach ($list as $k => $v) {
            $objData[$k] = $this->_post($k, $v);
        }
        $objData['infotime'] = TIME;
        $objData['stime'] = inTime($objData['stime']);
        $objData['etime'] = inTime($objData['etime']);
        $objData['status'] = intval($objData['status']);
        // $objData['px'] = intval($objData['px']);
        if($objData['game_id'] == -1) {
            $this->JsonReturn('请选择一款游戏');
        }
        if (!$objData['title']) {
            return $this->JsonReturn('标题为必填');
        }
        if (!$objData['img_url'] && !$objData['content']) {
            return $this->JsonReturn('图片地址或者内容必填一项');
        }
        return $objData;
    }


    function checkGameAdvs($data) {
        $where['game_id'] = $data['game_id'];
        $where['position_id'] = $data['position_id'];
        $where['stime[<=]'] = TIME;
        $where['etime[>=]'] = TIME;
        $where['status']    = 1;    
        $rs = M('advs')->field("id")->where($where)->find();
        if($rs) {
            return false;
        }else{
            return true;
        }
    }

}

function getStatusInfo($rs) {
    if (!$rs['status']) {
        return '<span class="gray">已下线</span>';
    }
    if ($rs['etime'] < TIME) {
        return '<span class="gray">已过期</span>';
    }
    if ($rs['stime'] > TIME) {
        return '<span class="red">未上线</span>';
    }
    return '正常';
}
